<?php

//apd_set_pprof_trace();
require_once('../includes/DB.class.php');
require_once('../includes/SmartyManager.class.php');
require_once('../includes/exceptions/AppExceptions.class.php');
require_once('../includes/SimpleTableExporter.class.php');

$filename = '库中_'.date('YmdHis').'.xls';

ini_set("max_execution_time",0);
ini_set( "memory_limit" , "128M");

try{
	$exp = new SimpleTableExporter($filename);
	$query = "select * from(
			(select distinct(certificationNumber),rukuDate from little_ruku where certificationCodification !='' and certificationCodification is not null and certificationNumber not in (select certificationNumber from little_chuku)) as rukuTable
			
			left outer join
			
			(select * from little_main) as mainTable
			
			using (certificationNumber)
		)";
//	$query = "select * from little_main,little_ruku 
//			where little_ruku.certificationNumber = little_main.certificationNumber 
//			and little_ruku.certificationNumber not in (select certificationNumber from little_chuku)";
	$certificationNumber = array();
	$certificationCodification = array();
	$sequenceNumber = array();
	$manufactory = array();
	$authenticationMark = array();
	$steelNumber =  array();
	$size1 = array();
	$size2 = array();
	$size3 = array();
	$count = array();
	$unitWeight = array();
	$weight = array();
	$orderNumber = array();
	$orderSubitemNumber = array();
	$unitPrice = array();
	$materialNumber = array();
	$consignmentBatch = array();
	$sampleBatch = array();
	$facheNumber = array();
	$purchaseNumber = array();
	$shipmentDate = array();
	$month = array();
	$vehicleNumber = array();
	$consignee = array();
	$remark = array();
	$rukuDate = array();

	DB::query($query);
	$result = DB::getResult();
	while($row = $result->fetch_assoc()){
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($sequenceNumber, $row['sequenceNumber']);
			array_push($manufactory, $row['manufactory']);
			array_push($authenticationMark, $row['authenticationMark']);
			array_push($steelNumber, $row['steelNumber']);	
			array_push($size1, $row['size1']);
			array_push($size2, $row['size2']);
			array_push($size3, $row['size3']);
			array_push($count, $row['count']);
			array_push($unitWeight, $row['unitWeight']);	
			array_push($weight,$row['count']*$row['unitWeight']);		
			array_push($orderNumber, $row['orderNumber']);
			array_push($orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($unitPrice,$row['unitPrice']);
			array_push($materialNumber, $row['materialNumber']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($sampleBatch,$row['sampleBatch']);
			array_push($facheNumber,$row['facheNumber']);
			array_push($purchaseNumber,$row['purchaseNumber']);
			array_push($shipmentDate,$row['shipmentDate']);
			array_push($month,$row['month']);
			array_push($vehicleNumber, $row['vehicleNumber']);
			array_push($consignee, $row['consignee']);
			array_push($remark, $row['remark']);
			array_push($rukuDate, $row['rukuDate']);
	}

	$exp->insertCustomData(array($certificationNumber,$facheNumber,$rukuDate,$certificationCodification,$sequenceNumber,$manufactory,$authenticationMark,$steelNumber,$size1,$size2,$size3,
							$count,$unitWeight,$weight,$orderNumber,$orderSubitemNumber,$unitPrice,$materialNumber,$consignmentBatch,$sampleBatch,$purchaseNumber,$shipmentDate,$month,$vehicleNumber,$consignee,$remark), 
							array('产品质量证明书号','发车证书号','入库日期','产品质量证明书编号','批次','生产厂家','船级','材质','厚','宽','长','数量','单重','重量',
							'订单号','订单子项号','受订单价','物料号','发货批次','批号','购单号','发货时间','合同月份','车号',
							'销售产家','备注'), 'sheet1');
	$exp->export();
	
}catch(Exception $e){
	$smarty = SmartyManager::getSmarty();
	$smarty->assign('errMsg', $e);
	$smarty->assign('errTitle', '查询数据库时出现错误');
	$smarty->display('error.html');
	die();
}
?>

